// ==UserScript==
// @name           mailto in SFEE
// @namespace      oasis.mitra.com
// @description    Add Email option in SFEE trackers
// @include        http://oasis.mitra.com*artf*
// ==/UserScript==

var artfTables = document.evaluate(
    "//table[@class='ItemDetailContainer']",
    document,
    null,
    XPathResult.ANY_TYPE,
    null);
    
var artfTable = artfTables.iterateNext();
var artfHtml = artfTable.innerHTML;
var preArtfNum = "";
var postArtfNum = "";
if( artfHtml.indexOf('Artifact artf') >= 0 )
{
    preArtfNum = artfHtml.indexOf('Artifact ') + 9;
    postArtfNum = artfHtml.indexOf(' : ');
}
var artfNum = artfHtml.substring(preArtfNum, postArtfNum);


var tables = document.evaluate(
    "//table[@class='Container']",
    document,
    null,
    XPathResult.ANY_TYPE,
    null);
    
var table = tables.iterateNext();

var logMsg = "Found: ";

while (table) {
    if( table.innerHTML.indexOf('Attachments:') >= 0 )
    {
  	logMsg += table.innerHTML + "\n";
  	break;
    }
    table = tables.iterateNext();
}

var trs = table.getElementsByTagName('TR');
var ourTR;

for( var i = 0; i < trs.length; i++ )
{
    if( trs[i].innerHTML.indexOf('Update And View') >= 0 )
    {
    	ourTR = trs[i];
    	break;
    }
}

var divs = ourTR.getElementsByTagName('DIV');
var div = divs[0];


var spaceDiv = document.createElement("div");
spaceDiv.setAttribute('class', 'ButtonSpace');
var newDiv = document.createElement("div");
newDiv.setAttribute('class', 'Button');
newDiv.innerHTML = '<div class="Left">&nbsp;</div><div class="Middle"><strong><a href="mailto:?cc=' + artfNum + '@oasis.mitra.com&body=%0D%0A%0D%0Ahttp://oasis.mitra.com/sf/go/' + artfNum +  '" id="email">Email</a></strong></div><div class="Right">&nbsp;</div>';

div.appendChild(spaceDiv);
div.appendChild(newDiv);